using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 拖拽预览控制器 - 管理拖拽时的预览效果
/// </summary>
public class DragPreviewController : MonoBehaviour
{
    [Header("预览设置")]
    public SpriteRenderer previewRenderer; // 预览精灵渲染器
    public Image previewImage; // 预览UI图片
    public Color validColor = Color.green; // 有效放置颜色
    public Color invalidColor = Color.red; // 无效放置颜色
    public Color normalColor = Color.white; // 正常颜色
    public float alpha = 0.7f; // 透明度
    
    [Header("动画设置")]
    public bool enablePulse = true; // 是否启用脉冲动画
    public float pulseSpeed = 2f; // 脉冲速度
    public float pulseRange = 0.1f; // 脉冲范围
    
    // 私有变量
    private Vector3 originalScale;
    private bool isPulsing = false;
    private float pulseTime = 0f;
    
    private void Awake()
    {
        // 获取组件引用
        if (previewRenderer == null)
            previewRenderer = GetComponent<SpriteRenderer>();
        
        if (previewImage == null)
            previewImage = GetComponent<Image>();
        
        // 保存原始缩放
        originalScale = transform.localScale;
        
        // 设置初始透明度
        SetAlpha(alpha);
    }
    
    private void Update()
    {
        if (enablePulse && isPulsing)
        {
            UpdatePulseAnimation();
        }
    }
    
    /// <summary>
    /// 设置预览精灵
    /// </summary>
    /// <param name="sprite">精灵</param>
    public void SetPreviewSprite(Sprite sprite)
    {
        if (previewRenderer != null)
        {
            previewRenderer.sprite = sprite;
        }
        
        if (previewImage != null)
        {
            previewImage.sprite = sprite;
        }
    }
    
    /// <summary>
    /// 设置预览颜色
    /// </summary>
    /// <param name="color">颜色</param>
    public void SetPreviewColor(Color color)
    {
        Color newColor = color;
        newColor.a = alpha;
        
        if (previewRenderer != null)
        {
            previewRenderer.color = newColor;
        }
        
        if (previewImage != null)
        {
            previewImage.color = newColor;
        }
    }
    
    /// <summary>
    /// 设置有效放置状态
    /// </summary>
    public void SetValidState()
    {
        SetPreviewColor(validColor);
        StartPulse();
    }
    
    /// <summary>
    /// 设置无效放置状态
    /// </summary>
    public void SetInvalidState()
    {
        SetPreviewColor(invalidColor);
        StartPulse();
    }
    
    /// <summary>
    /// 设置正常状态
    /// </summary>
    public void SetNormalState()
    {
        SetPreviewColor(normalColor);
        StopPulse();
    }
    
    /// <summary>
    /// 设置透明度
    /// </summary>
    /// <param name="alpha">透明度值</param>
    public void SetAlpha(float alpha)
    {
        this.alpha = alpha;
        
        if (previewRenderer != null)
        {
            Color color = previewRenderer.color;
            color.a = alpha;
            previewRenderer.color = color;
        }
        
        if (previewImage != null)
        {
            Color color = previewImage.color;
            color.a = alpha;
            previewImage.color = color;
        }
    }
    
    /// <summary>
    /// 开始脉冲动画
    /// </summary>
    public void StartPulse()
    {
        isPulsing = true;
        pulseTime = 0f;
    }
    
    /// <summary>
    /// 停止脉冲动画
    /// </summary>
    public void StopPulse()
    {
        isPulsing = false;
        transform.localScale = originalScale;
    }
    
    /// <summary>
    /// 更新脉冲动画
    /// </summary>
    private void UpdatePulseAnimation()
    {
        pulseTime += Time.deltaTime * pulseSpeed;
        
        float scale = 1f + Mathf.Sin(pulseTime) * pulseRange;
        transform.localScale = originalScale * scale;
    }
    
    /// <summary>
    /// 设置预览大小
    /// </summary>
    /// <param name="size">大小</param>
    public void SetPreviewSize(Vector3 size)
    {
        transform.localScale = size;
        originalScale = size;
    }
    
    /// <summary>
    /// 设置预览旋转
    /// </summary>
    /// <param name="rotation">旋转角度</param>
    public void SetPreviewRotation(float rotation)
    {
        transform.rotation = Quaternion.Euler(0, 0, rotation);
    }
    
    /// <summary>
    /// 显示预览
    /// </summary>
    public void Show()
    {
        gameObject.SetActive(true);
    }
    
    /// <summary>
    /// 隐藏预览
    /// </summary>
    public void Hide()
    {
        gameObject.SetActive(false);
        StopPulse();
    }
    
    /// <summary>
    /// 复制物品的外观到预览
    /// </summary>
    /// <param name="sourceObject">源物品</param>
    public void CopyAppearanceFrom(GameObject sourceObject)
    {
        if (sourceObject == null) return;
        
        // 复制精灵
        SpriteRenderer sourceRenderer = sourceObject.GetComponent<SpriteRenderer>();
        if (sourceRenderer != null && sourceRenderer.sprite != null)
        {
            SetPreviewSprite(sourceRenderer.sprite);
        }
        
        // 复制大小
        SetPreviewSize(sourceObject.transform.localScale);
        
        // 复制旋转
        SetPreviewRotation(sourceObject.transform.eulerAngles.z);
    }
} 